package lv.accenture.bootcamp;

public class Hilbert {


    public String sampleSVG(int order) {
    	
    	int size = (int) Math.pow(2, order);
    	
       	StringBuffer buf = new StringBuffer();
        buf.append("<?xml version='1.0'?>\n");
        buf.append("<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'\n");
        buf.append("'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>\n");
        buf.append("<svg width='50cm' height='50cm' viewBox='0 0 " + size + " " + size + "'>\n");
        buf.append("<title>Hilbert curve</title>\n");
        buf.append("<desc>This is a sample SVG file for Hilbert curve</desc>\n");
        buf.append("<rect x='0' y='0' width='" + size + "' height='" + size + "' fill='#fdfdfd'/>\n");
        buf.append("<g transform=\"translate(0," + size + ") scale(1 -1)\" style=\"stroke:#006699;stroke-width:0.07;fill:#c0c0c0;\">\n");
        buf.append(hilbertSVG(order) + "\n");
        buf.append("</g>\n");
        buf.append("</svg>\n");
        
        return buf.toString();

	}
  
    // object rotation method
    public String rotate(String S, String side){
    	String[] sa = new String[]{"h1","h-1","v1","v-1"};
    	String[] sa2 = null;
    	
    	if(side=="right"){    		
        	sa2 = new String[]{"V1","V-1","H1","H-1"};
    	}
    	else if(side=="left"){    		
        	sa2 = new String[]{"V-1","V1","H-1","H1"};
    	}
    	
     	for(int i=0; i<sa.length; i++){
    		S = S.replaceAll(sa[i], sa2[i]);
    		
    	}	
     	
     	S = S.toLowerCase();    
    	
    	return S;
    }

     
	public String hilbertSVG(int order) {
		String begin  = new String("v1,h1,v-1");		
		   	for (int i=1; i < order; i++) {		
				    			
			begin = rotate(begin, "right") + ",v1," + begin + ",h1," + begin + ",v-1," + rotate(begin, "left");
		}
		return "<path d=\"M,0.5,0.5," + begin + "\"/>";
	}
}